import { Controller, Post, Get,Param, ParseIntPipe, Body ,BadRequestException, Delete, Query } from '@nestjs/common';
import { success,error } from '@/utils';
import { DishesService  } from './dishes.service';
@Controller('dishes')
export class DishesController {
    constructor(private readonly dishesService: DishesService) {}
    @Post('list')
    async list(@Body() info:any) {
        try {   
          const res = await this.dishesService.find(info)
          return success(res, '获取成功');
        } catch (error) {
            throw new BadRequestException('参数不正确');
        }
    }
    @Post('listBy')
    async listBy(@Body() info:any) {
        try {   
          const res = await this.dishesService.findBy(info)
          return success(res, '获取成功');
        } catch (error) {
            throw new BadRequestException('参数不正确');
        }
    }
    @Post('editBy')
    async editBy(@Body() info:any) {
        try { 
          if(info.id){
            var res = await this.dishesService.edit(info)
          }else{
            var res = await this.dishesService.add(info)
          }
          return success(res,info.id?'修改成功':'获取成功');
        } catch (error) {
            throw new BadRequestException('参数不正确');
        }
    }
    @Delete('delete')
    async delete(@Query('id',ParseIntPipe) id:number) {
      try { 
        const res = await this.dishesService.delete(id)
        return success(res, '删除成功');
      } catch (error) {
          throw new BadRequestException('参数不正确');
      }
    }
    @Get('detail/:id')
    async detail(@Param('id',ParseIntPipe) id: number){
        const res =  await this.dishesService.detail(id)
        return success(res, '获取成功');
    }
}
